<template>
    <div class="album-page">
        <div class="turn">
            <div class="albumlist">
                <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
                <van-list
                  v-model="loading"
                  :finished="finished"
                  finished-text="没有更多了"
                  @load="onLoad"
                >
                <div class="albumItem" v-for="item in list" :key="item.id" @click="goSortList(item.id)">
                    <div class="img" :style="{ 'background-image': 'url(https://pic.ecook.cn/web/'+item.imageid+'.jpg!wl280)' }"
                    v-lazy:background-image="'https://pic.ecook.cn/web/'+item.imageid+'.jpg!wl280'"
                    >
                        <div class="name">{{item.name}}</div>
                        <div class="tips" v-if="item.cookbookSum">共{{item.cookbookSum}}道菜</div>
                        <div class="tips" v-else>共0道菜</div>
                    </div>
                </div>
                </van-list>
                </van-pull-refresh>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    props:['albumList'],
    data() {
        return {
            list: [],
            loading: false,
            finished: false,
            refreshing: false,
            num:0,
        };
    },
    methods: {
    onLoad() {
      setTimeout(() => {
        if (this.refreshing) {
          this.list = [];
          this.refreshing = false;
          this.num = 0
        }

        if (this.albumList.length>=10) {
          for (let i = this.num; i < this.num + 10; i++) {
            if (this.albumList[i]!=undefined) {           
              this.list.push(this.albumList[i]);
            }
          }
        }else if(this.albumList.length<10){
          for (let i = this.num; i < this.albumList.length; i++) {
            this.list.push(this.albumList[i]);
          }
        }
      
        this.num += 10
        this.loading = false;

        if (this.list.length >= this.albumList.length) {
          this.finished = true;
        }
        }, 500);
    },
    onRefresh() {
        // 清空列表数据
        this.finished = false
        // 重新加载数据
        // 将 loading 设置为 true，表示处于加载状态
        this.loading = true;
        this.onLoad();
    },
    //跳转专辑页面
    goSortList(id){
      this.$router.push({ path: "/novice", query: { id } });
    }
    },
    created() {
        
    },
};
</script>

<style lang="less" scoped>
.album-page{
    width: 100%;
    background-color: #f2f2f2;

    .turn{
        margin-top: 25px;

        .albumlist{
            padding: 15px 20px;

            .albumItem{
                width: 100%;
                height: 160px;
                margin-bottom: 20px;

                .img{
                    width: 100%;
                    height: 100%;
                    background-position: center center;
                    background-repeat: no-repeat;
                    background-size: cover;
                    border-radius: 8px;
                    position: relative;
                    
                    .name{
                        display: inline-block;
                        color: #fff;
                        font-weight: bold;
                        position: absolute;
                        bottom: 0;
                        left: 0;
                        padding: 0 0 15px 10px;
                        letter-spacing: 0.5px;
                        font-size: 17px;
                    }
                    .tips{
                        display: inline-block;
                        border-radius: 8px;
                        color: #fff;
                        font-size: 12px;
                        padding: 5px 8px;
                        background-color: rgba(0, 0, 0, 0.4);
                        position: absolute;
                        top: 8px;
                        right: 8px;
                        letter-spacing: 0.8px;
                        font-weight: bold;
                    }
                    
                }
            }

        }
    }
}
</style>